#!/usr/bin/env python3
# encoding: utf-8
# ===============================================================================
#
#         FILE:
#
#        USAGE:
#
#  DESCRIPTION:
#
#      OPTIONS:  ---
# REQUIREMENTS:  ---
#         BUGS:  ---
#        NOTES:  ---
#       AUTHOR:  x2x4com@gmail.com
#      COMPANY:  DataGeek
#      VERSION:  1.0
#      CREATED:  2019-12-04
#     REVISION:  ---
# ===============================================================================
from random import randint
import codecs

ds = {
    'kpi_d_setrcr': {
        'index': '{"index":{"_index":"kpi_d_setrcr","_type":"system"}}',
        'kpicode': [
            '11001',
            '11001001',
            '11001002',
            '11002',
            '11002001',
            '11002002',
            '11002003',
            '11002004',
            '11002005',
            '11002006',
            '11003',
            '11003001',
            '11003002',
            '11003003',
            '11003004',
            '11004',
            '11004001',
            '11004002',
            '11005',
            '11006',
            '11007',
        ]
    },
    'kpi_d_stldr': {
        'index': '{"index":{"_index":"kpi_d_stldr","_type":"system"}}',
        'kpicode': [
            '13001',
            '13001001',
            '13001002',
            '13002',
            '13003',
            '13003001',
            '13003002',
            '13003003'
        ]
    },
    'kpi_d_stlsr': {
        'index': '{"index":{"_index":"kpi_d_stlsr","_type":"system"}}',
        'kpicode': [
            '9001',
            '9001001',
            '9001002',
            '9002',
            '9003',
            '9003001',
            '9003002',
            '9003003',
        ]
    },
    'kpi_d_ubuvr': {
        'index': '{"index":{"_index":"kpi_d_ubuvr","_type":"system"}}',
        'kpicode': [
            '12001',
            '12001001',
            '12001002',
            '12001003',
            '12001004',
            '12002',
            '12002001',
            '12002002',
            '12003',
            '12003001',
            '12003002'
        ]
    }
}

period = ['2019-12-%s 00:00:00.000' % x for x in range(24, 32)]
period += ['2020-01-%s 00:00:00.000' % x for x in range(1, 32)]


def kpi_d_setrcr(day) -> list:
    _ds = ds['kpi_d_setrcr']
    _i = _ds['index']
    _rt = list()
    print('生成%s的es_kpi_d_setrcr' % day)
    for kpicode in _ds['kpicode']:
        msg = "请查看异常交易根因算法模块系统"
        _roll = randint(0, 5)
        if _roll == 0:
            msg = ""
        _rt.append(_i)
        _rt.append(
            '{"kpicode":"%s","kpidate":"%s","tfrc":%d,"tfrcs":"%s"}' % (
                kpicode,
                day,
                _roll,
                msg
            )
        )
    return _rt


def kpi_d_stldr(day) -> list:
    _ds = ds['kpi_d_stldr']
    _i = _ds['index']
    _rt = list()
    _data = dict()
    print('生成%s的kpi_d_stldr' % day)
    # 先预生成一遍数据，写入字典
    for kpicode in _ds['kpicode']:
        _max = randint(4, 5)
        _min = randint(1, 3)
        # _avg = randint(3, 4)
        _avg = int((_max + _min) / 2)
        if kpicode not in _data:
            _data[kpicode] = {"kpicode": kpicode,
                              "kpidate": day,
                              "maxd": _max,
                              "mind": _min,
                              "avgd": _avg}

    # 额外的硬逻辑
    _data['13001'] = _data['13001001'].copy()
    _data['13001']['kpicode'] = '13001'
    _data['13003']['maxd'] = max(_data['13003001']['maxd'], _data['13003002']['maxd'])
    _data['13003']['mind'] = min(_data['13003001']['mind'], _data['13003002']['mind'])
    _data['13003']['avgd'] = int((_data['13003']['maxd'] + _data['13003']['mind']) / 2)

    # 根据定义输出
    for kpi in _ds['kpicode']:
        _rt.append(_i)
        _rt.append(
            '{"kpicode":"%s","kpidate":"%s","maxd":%d,"mind":%d,"avgd":%d,"emaxd":"","emind":"","eavgd":"","cmaxd":"","cmind":"","cavgd":"","pmaxd":"","pmind":"","pavgd":""}' % (
                _data[kpi]['kpicode'],
                _data[kpi]['kpidate'],
                _data[kpi]['maxd'],
                _data[kpi]['mind'],
                _data[kpi]['avgd']
            )
        )

    return _rt


def kpi_d_stlsr(day) -> list:
    _ds = ds['kpi_d_stlsr']
    _i = _ds['index']
    _rt = list()
    print('生成%s的es_kpi_d_stlsr' % day)
    for kpicode in _ds['kpicode']:
        # 总量>成功量。总量：500,000+-100,000。成功量=总量-(1000至6000)
        # tnt 总量  nst 成功量
        _rate = randint(0, 9)
        if randint(0, 1) == 1:
            # 多
            _tnt = int((100 + (1-randint(85, 99) / 100))/100*500000)
        else:
            # 少
            _tnt = int((99 + randint(85, 99) / 100)/100*500000)
        _nst = _tnt - randint(1000, 6000)

        _rt.append(_i)
        _rt.append(
            '{"kpicode":"%s","kpidate":"%s","nst":%d,"tnt":%d,"enst":"","etnt":"","cnst":"","ctnt":"","pnst":"","ptnt":"","faildTrade":""}' % (
                kpicode,
                day,
                _nst,
                _tnt
            )
        )
    return _rt


def kpi_d_ubuvr(day) -> list:
    _ds = ds['kpi_d_ubuvr']
    _i = _ds['index']
    _rt = list()
    print('生成%s的es_kpi_d_ubuvr' % day)
    for kpicode in _ds['kpicode']:
        msg = "请查看原始数据查询系统"
        _roll = randint(1, 100)
        if _roll == 0:
            msg = ""
        _rt.append(_i)
        _rt.append(
            '{"kpicode":"%s","kpidate":"%s","ubrv":%s,"abuacc":"%s","abupri":"%s","dataleak":"%s","terinf":"%s"}' % (
                kpicode,
                day,
                _roll,
                msg,
                msg,
                msg,
                msg
            )
        )
    return _rt


def main():
    for k in ds.keys():
        _file_name = '%s.json' % k
        print('开始生成%s.json' % _file_name)
        data = list()
        for _p in period:
            data += eval(k)(_p)
        str_data = ''
        for _d in data:
            str_data += str(_d) + '\n'
        # str_data = str_data.encode('utf8')
        print('%s 数据填充完毕，写入文件' % k)
        with codecs.open(_file_name, 'w', 'utf8') as fd:
            fd.write(str_data)
        print('%s.json写入完成' % _file_name)


if __name__ == '__main__':
    main()

